package com.pe1n.yangtaopai.page.home.view.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyJingxuanAdapter;
import com.pe1n.yangtaopai.adapter.MyMeizhuangAdapter;
import com.pe1n.yangtaopai.bean.BrandBean;
import com.pe1n.yangtaopai.bean.FindBean;
import com.pe1n.yangtaopai.bean.MultiTitleBean;
import com.pe1n.yangtaopai.bean.NewsBean;
import com.pe1n.yangtaopai.bean.OtherBean;
import com.pe1n.yangtaopai.bean.PreferenceBean;
import com.pe1n.yangtaopai.bean.RecommendBean;
import com.pe1n.yangtaopai.bean.SingleTitleBean;
import com.pe1n.yangtaopai.bean.SpecialBean;
import com.pe1n.yangtaopai.bean.TypeBean;
import com.pe1n.yangtaopai.page.home.presenter.PresenterImpl;
import com.pe1n.yangtaopai.page.home.view.interfacese.IView;
import com.pe1n.yangtaopai.utils.URLConstant;

import java.util.ArrayList;
import java.util.List;

public class MeizhuangFragment extends Fragment implements IView{

    private Context mContext;
    private PresenterImpl mPresenter;
    private XRecyclerView xRecyclerView;
    private ProgressBar progressBar;
    private List<Object> mList = new ArrayList<>();
    private MyMeizhuangAdapter adapter;
    private List<OtherBean.DataBean> mOtherBeanList = new ArrayList<>();
    private int page = 1;
    private String urlTitle;

    public MeizhuangFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_meizhuang, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.xrecview_meizhuang);
        progressBar = (ProgressBar)view.findViewById(R.id.progress_bar_meizhuang);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new MyMeizhuangAdapter(mContext,mList,mOtherBeanList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        xRecyclerView.setLayoutManager(layoutManager);
        xRecyclerView.setAdapter(adapter);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mList.clear();
                page = 1;
                mPresenter.loadMultiTitleData(urlTitle);
            }

            @Override
            public void onLoadMore() {
                page++;
                String urlOther = String.format(URLConstant.OTHER_URL,page,2,10);
                mPresenter.loadOtherData(urlOther);
            }
        });
        mPresenter = new PresenterImpl(this);
        urlTitle = String.format(URLConstant.PUBLIC_URL,14);
        mPresenter.loadMultiTitleData(urlTitle);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
        xRecyclerView.refreshComplete();
        xRecyclerView.loadMoreComplete();
    }

    @Override
    public void showLoadFailMsg() {

    }

    @Override
    public void loadMultiTitleData(MultiTitleBean multiTitleBean) {
        mList.add(multiTitleBean);
        adapter.notifyDataSetChanged();
        String typeUrl = String.format(URLConstant.PUBLIC_URL,15);
        mPresenter.loadTypeData(typeUrl);
    }

    @Override
    public void loadSingleTitleData(SingleTitleBean singleTitleBean) {

    }

    @Override
    public void loadBrandData(BrandBean brandBean) {
        mList.add(brandBean);
        adapter.notifyDataSetChanged();
        String recommendUrl = String.format(URLConstant.PUBLIC_URL,61);
        mPresenter.loadRecommendData(recommendUrl);
    }

    @Override
    public void loadFindData(FindBean findBean) {

    }

    @Override
    public void loadNewsData(NewsBean newsBean) {

    }

    @Override
    public void loadOtherData(OtherBean otherBean) {
        if (page == 1) {
            mList.add(otherBean);
        }
        List<OtherBean.DataBean> beanList = otherBean.getData();
        mOtherBeanList.addAll(beanList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadPreferenceData(PreferenceBean preferenceBean) {
        mList.add(preferenceBean);
        adapter.notifyDataSetChanged();
        String brandUrl = String.format(URLConstant.PUBLIC_URL,60);
        mPresenter.loadBrandData(brandUrl);
    }

    @Override
    public void loadRecommendData(RecommendBean recommendBean) {
        mList.add(recommendBean);
        adapter.notifyDataSetChanged();
        String otherUrl = String.format(URLConstant.OTHER_URL,1,2,30);
        mPresenter.loadOtherData(otherUrl);
    }

    @Override
    public void loadTypeData(TypeBean typeBean) {
        mList.add(typeBean);
        adapter.notifyDataSetChanged();
        String preferenceUrl = String.format(URLConstant.PUBLIC_URL,59);
        mPresenter.loadPreferenceData(preferenceUrl);
    }

    @Override
    public void loadSpecialData(SpecialBean specialBean) {

    }

}
